import express from 'express';
import { fomaterStr } from '../../util/util.js'
import * as articles from '../../controllers/articles.js'
import * as comments from '../../controllers/comment.js'
const router = express.Router();

// router.get('/', (req, res) => {
//   res.send('This is the API root!');
// });

//文章搜索
router.get("/search", async (req, res) => {
  let page = req.query.page;
  if (page == null || page == "") {
    page = 1;
  }
  let perpage = req.query.perpage;
  if (perpage == null || perpage == "") {
    perpage = 6;
  }
  //计算从第几条开始进行查询
  let startIndex = (page - 1) * perpage;

  let type = req.query.type;
  let key = req.query.key;

  let condition = {
    equies: {},
    like: {}
  };
  if (type != null && type != "") {
    condition.equies.categoryId = type;
  }
  if (key != null && key != "") {
    condition.like.title = key;
  }
  let data = await articles.selectConditionArticle2(startIndex, perpage, condition);
  res.send({
    code: 200,
    msg: "搜索成功",
    data
  })
});

//热门图片
router.get("/hotpic", async (req, res) => {
  let [{ count }] = await articles.selectArticlesCount();
  let start = Math.floor(Math.random() * (count - 5));
  let data = await articles.selectArticlesRandom(start);
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
});

router.get("/category", async (req, res) => {
  // let [{ name }] = await articles.selectArticlesCatrgory();
  let data = await articles.selectArticlesCatrgory();
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
})


router.get("/rank", async (req, res) => {
  let data = await articles.selectArticlesRank();
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
})

router.get("/latest", async (req, res) => {
  let data = await articles.selectArticlesLatest();
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
})

router.get("/latest_comment", async (req, res) => {
  let data = await articles.selectArticlesComment();
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
})


router.get("/attention", async (req, res) => {
  let [{ count }] = await articles.selectArticlesCount();
  let start = Math.floor(Math.random() * (count - 7));
  let data = await articles.selectArticlesAttention(start);
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
});

//查询文章详情
router.get("/article", async (req, res) => {
  let id = req.query.id;
  if (id == null || id == "") {
    res.send({
      code: 400,
      msg: "文章id参数有误"
    });
    return;
  }

  let [data] = await articles.selectArticleDetail(id);
  if (data == null) {
    res.send({
      code: 400,
      msg: "文章id参数有误"
    });
    return;
  }

  let [preData] = await articles.selectPreArticle(id);
  let [nextData] = await articles.selectNextArticle(id);

  data.pre = preData === undefined ? null : preData;
  data.next = nextData === undefined ? null : nextData;
  //返回查询结果
  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
});


//发表评论
router.post("/post_comment", async (req, res) => {
  let author = req.body?.author ?? "";
  let content = req.body?.content ?? "";
  let articleId = req.body?.articleId ?? "";
  // console.log(author,content,articleId);

  let [{ count }] = await articles.selectArticlesCount();
  if (author == "" ||
    content == "" ||
    articleId == "" || articleId < 1 || articleId > count
  ) {
    res.send({
      code: 400,
      msg: "参数错误"
    });
    return;
  }

  await comments.insertNewComment(author, content, articleId);

  res.send({
    code: 200,
    msg: "评论发表成功"
  })

});

//评论列表
router.get("/get_comment", async (req, res) => {
  let articleId = req.query?.articleId ?? "";

  if (articleId == "") {
    res.send({
      code: 400,
      msg: "文章ID参数错误"
    });
    return;
  }
  let data = await comments.selectComment(articleId);

  res.send({
    code: 200,
    msg: "查询成功",
    data
  })
});



export default router;
